<?php
/**
 * DBShop 电子商务系统
 *
 * ==========================================================================
 * @link      https://www.dbshop.net/
 * @copyright 北京珑大钜商科技有限公司，并保留所有权利。
 * @license   https://www.dbshop.net/license.html License
 * ==========================================================================
 *
 * @author    静静的风 <baron@loongdom.cn>
 *
 */

namespace Admin\Service\Common\PhoneSms;

use Laminas\Config\Reader\Xml;
use Laminas\Http\Client;

class HuyiSms
{
    private $sendUrl = 'https://106.ihuyi.com/webservice/sms.php?method=Submit';
    private $smsConfig;

    /**
     * 配置信息
     * @param $smsConfig
     */
    public function setSmsConfig($smsConfig)
    {
        $this->smsConfig = $smsConfig;
    }

    /**
     * 发送短信
     * @param array $data
     */
    public function sendPhoneSms(array $data)
    {
        $smsContent = $this->getSmsContent($data['typeCode'], $data['contentArray'], $data['templateCode']);
        if (empty($smsContent)) return;
        if (empty($data['phone'])) return;

        foreach ($data['phone'] as $phoneValue) {
            $this->sendHuyiSms($phoneValue['number'], $smsContent);
        }
    }

    /**
     * 发送订单短信
     * @param array $data
     */
    public function sendOrderPhoneSms(array $data)
    {
        $smsContent = $this->getSmsContent($data['typeCode'], $data['contentArray'], $data['templateCode']);
        if (empty($smsContent)) return;
        if (empty($data['phone'])) return;

        foreach ($data['phone'] as $phoneValue) {
            $this->sendHuyiSms($phoneValue['number'], $smsContent);
        }
    }

    /**
     * 发送短信方法
     * @param $phone
     * @param $content
     * @return array|bool|string|null
     */
    private function sendHuyiSms($phone, $content)
    {
        $client = new Client($this->sendUrl, [
            'adapter' => 'Laminas\Http\Client\Adapter\Curl',
            'curloptions' => [
                CURLOPT_SSL_VERIFYPEER => false
            ]
        ]);
        $client->setMethod('POST');
        $client->setParameterPost([
            'account'   => $this->smsConfig['sms_id'],
            'password'  => $this->smsConfig['sms_secret'],
            'mobile'    => $phone,
            'content'   => $content,
            'share_code'=> 'QG7Udt'
        ]);

        try {
            $response = $client->send();
            if ($response->isSuccess()) {
                $xml = new Xml();
                return $xml->fromString($response->getBody());
            }
        } catch (\Exception $e) {
            return null;
        }

        return null;
    }

    /**
     * 获取短信内容
     * @param $type
     * @param $data
     * @param $templateCode
     * @return string
     */
    private function getSmsContent($type, $data, $templateCode)
    {
        $smsContent = '';
        $TemplateCodeContent = '';
        switch ($type) {
            case 'captcha':
                if (!is_int($templateCode)) $TemplateCodeContent = str_replace('{code}', $data['phoneCaptcha'], $templateCode);
                $smsContent = ($templateCode == $TemplateCodeContent || empty($TemplateCodeContent)) ? '您的验证码为'.$data['phoneCaptcha'].'切勿告知他人' : $TemplateCodeContent;
                break;
            case 'addOrder':
                if (!is_int($templateCode)) $TemplateCodeContent = str_replace(['{buyName}', '{orderSn}', '{orderTotal}'], [$data['buyName'], $data['orderSn'], $data['orderTotal']], $templateCode);
                $smsContent = ($templateCode == $TemplateCodeContent || empty($TemplateCodeContent)) ? '亲爱的'.$data['buyName'].'，您的订单已提交，订单金额为'.$data['orderTotal'].'，请及时进行后续处理' : $TemplateCodeContent;
                break;
            case 'cancelOrder':
                if (!is_int($templateCode)) $TemplateCodeContent = str_replace(['{buyName}', '{orderSn}', '{orderTotal}'], [$data['buyName'], $data['orderSn'], $data['orderTotal']], $templateCode);
                $smsContent = ($templateCode == $TemplateCodeContent || empty($TemplateCodeContent)) ? '亲爱的'.$data['buyName'].'，您的订单'.$data['orderSn'].'，已经取消' : $TemplateCodeContent;
                break;
            case 'paymentOrder':
                if (!is_int($templateCode)) $TemplateCodeContent = str_replace(['{buyName}', '{orderSn}', '{orderTotal}'], [$data['buyName'], $data['orderSn'], $data['orderTotal']], $templateCode);
                $smsContent = ($templateCode == $TemplateCodeContent || empty($TemplateCodeContent)) ? '亲爱的'.$data['buyName'].'，您的订单'.$data['orderSn'].'，金额'.$data['orderTotal'].'，已经付款完成' : $TemplateCodeContent;
                break;
            case 'shipOrder':
                if (!is_int($templateCode)) $TemplateCodeContent = str_replace(['{buyName}', '{orderSn}', '{orderTotal}', '{deliveryName}', '{expressNumber}'], [$data['buyName'], $data['orderSn'], $data['orderTotal'], $data['deliveryName'], $data['expressNumber']], $templateCode);
                $smsContent = ($templateCode == $TemplateCodeContent || empty($TemplateCodeContent)) ? '亲爱的'.$data['buyName'].'，您的订单'.$data['orderSn'].'，已由快递'.$data['deliveryName'].'发出，快递单号'.$data['expressNumber'] : $TemplateCodeContent;
                break;
            case 'receiptOrder':
                if (!is_int($templateCode)) $TemplateCodeContent = str_replace(['{buyName}', '{orderSn}', '{orderTotal}', '{deliveryName}', '{expressNumber}'], [$data['buyName'], $data['orderSn'], $data['orderTotal'], $data['deliveryName'], $data['expressNumber']], $templateCode);
                $smsContent = ($templateCode == $TemplateCodeContent || empty($TemplateCodeContent)) ? '亲爱的'.$data['buyName'] .'，您的订单'.$data['orderSn'].'，已经签收' : $TemplateCodeContent;
                break;
        }

        return $smsContent;
    }
}